﻿
$(function () {
    toastr.options.positionClass = 'toast-bottom-center';
    toastr.options.time = 10000; 
    // 自定义验证
    var checkMaxCount = (rule, value, callback) => {
        if (!value) {
            return callback(new Error('请填写最大检查人数'))
        }
        if (!Number.isInteger(Number(value))) {
            return callback(new Error('必须为整数'));
        }
        callback();
    };

    var vm = new Vue({
        el: '#app',
        components: {
            // 局部注册组件
            'pool-box': PoolBoxComponent,
            'device-selector': DeviceSelectorComponent,
            'apply-form': ApplyFormComponent
        },
        computed: {
            workLoad: function () {
                var vm = this;
                var max = 0;
                vm.selectedNoAppointRows.forEach(item => {
                    if (item.workLoad > max) {
                        max = item.workLoad;
                    }
                })
                return max;
            },
            showCancel: function () {
                var vm = this;
                var list = vm.selectedAppointedRows.filter(item => item.appointmentStatus === 2);
                var res ;
                if (vm.selectedAppointedRows && vm.selectedAppointedRows.length > 0 && list.length === 0) {
                    res = true;
                } else {
                    res = false;
                }
                return res;
            }
        },
        watch: {
            date: function (value) {
                var vm = this;
                if (vm.activeTabName === 'noAppoints') {
                    vm.noAppointDate = value;
                } else {
                    vm.appointedDate = value;
                }
            },
            deviceId: function (value) {
                const vm = this;
                if (value) {
                    vm.getDateLimit();
                }
                else {
                    abp.ajax({
                        type: 'get',
                        url: `/api/custom-setting/get-setting-detail?level=0`
                    }).then(res => {
                        if (res.code == 0) {
                            vm.limitDay = res.data.appointableCount;
                        }
                    });
                }
            }
        },
        data() {

            return {
                // 角色与权限
                manulaPermission: true, // 能进到本页面，就是true

                /*--搜索部分 --*/
                formLabelWidth: '100px',
                searchModel: {
                    number: null,
                    userData: null
                },
                number: null,  // 查询输入框

                dateTimePickerDisable: true,

                // 范围快捷选项
                timeRangeOptions: [
                    { label: '今天', value: 'today', effect: 'dark' },
                    { label: '2天内', value: 'in2days', effect: 'plain' },
                    { label: '3天内', value: 'in3days', effect: 'plain' },
                    { label: '一周内', value: 'in1week', effect: 'plain' },
                    { label: '自定义', value: 'custom', effect: 'plain' },
                ],
                // 被选中的快捷范围
                selectedRangeIndex: 0,
                selectedRange: null,

                // 申请科室选项
                applyDepartmentOptions: [],

                // 过滤对象
                userData: {
                    name: null,            // 姓名
                    applyTimeRange: null,  // 申请时间范围
                    range:[],
                    medicalCardNumber: null,// 就诊卡号
                    outPatientNumber: null,
                    inPatientNumber: null,
                    physicalExamNumber: null,
                    phoneNumber: null,
                    idCardNumber: null,
                    applyNumber: null,
                    applyDepartments: null,
                    medicalInsuranceNumber: null
                },
                /*--------end---------*/

                /*--表格部分--*/
                patientName:null,

                collapseItemOpened: false,
                activeTabName: 'noAppoints', // 当前操作 tab 名称
                noAppoints: [],
                noAppointDate: null,
                noAppointDeviceType:null,
                noAppointDeviceId: null,
                noAppointSelectedPoolId: null,
                noAppointSelectedPoolName:null,
                noAppointSelectableIds: [], // 未预约可选中行id数组
                selectedNoAppointRows: [],    // 未预约被选中的行
                noAppointLoading: false,
                reserveSelectionNoAppoint: true,

                appointedPatinetName:null,
                appointeds: [],           // 已预约表数据
                appointedDate: null,
                appointedDeviceType:null,
                appointedDeviceId:null,
                appointedSelectedPoolId: null,
                appointedSelectedPoolName:null,
                appointedSelectableIds: [],  // 已预约可选中
                selectedAppointedRows: [],      // 已预约被选中
                appointedLoading: false,
                reserveSelectionAppointed: true,

                tableHeight: 450,
                /*------end-----------*/

                /*---------号池面板部分------------*/
                deviceTypeId: null,  // 选中设备类型Id
                deviceId: null, // 选中的设备ID
                pools: [],              // 号池
                date: null,
                limitDay: 30,          // 往后可预约天数  
                defaultAppointDate: null,// 日期
                selectedPoolName: null,
                selectedPoolId: null,
                // 日期选择配置
                pickerOptions: {
                    disabledDate(time) {
                        const date = new Date();
                        return time.getTime() < (Date.now() - (24 * 60 * 60 * 1000)) || time.getTime() > date.setTime(date.getTime() + 3600 * 1000 * 24 * vm.limitDay);
                    },
                    shortcuts: [{
                        text: '今天',
                        onClick(picker) {
                            picker.$emit('pick', new Date());
                        }
                    }, {
                        text: '明天',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() + 3600 * 1000 * 24);
                            picker.$emit('pick', date);
                        }
                    }, {
                        text: '后天',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() + 3600 * 1000 * 24 * 2);
                            picker.$emit('pick', date);
                        }
                    }, {
                        text: '大后天',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() + 3600 * 1000 * 24 * 2);
                            picker.$emit('pick', date);
                        }
                    }]
                },
                /*------------end------------------*/

                /*------------申请单弹窗-------------------*/
                selectedApplyForms: [],
                /*------------end--------------------------*/


                valueFormat: 'HH:mm',
                timePickerFormat: 'HH:mm a',

                // 批量 TimePickerOption
                batchStartTimePockerOption: {
                    selectableRange: '00:00:00-23:59:59',
                    format: 'HH:mm a'
                },
                batchEndTimePickerOption: {
                    selectableRange: '00:00:00-23:59:59',
                    format: 'HH:mm a'
                },

                // 验证规则
                rules: {
                    maxCount: [
                        { validator: checkMaxCount, trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            search() {
                var vm = this;
                vm.date = new Date();
                let startTime = dayjs(vm.userData.applyTimeRange[0]).format('YYYY-MM-DD HH:mm:ss');
                let endTime = dayjs(vm.userData.applyTimeRange[1]).format('YYYY-MM-DD HH:mm:ss');
                vm.userData.range = [startTime, endTime]
                vm.noAppointDate = vm.date;
                vm.noAppointSelectedPoolId = null;
                vm.appointedDate = vm.date;
                vm.appointedSelectedPoolId = null;
                vm.loadNoAppointData(true);
                vm.loadAppointedData(true);
            },

            // 获取推荐号池
            getPools() {
                var vm = this;
                vm.$refs['pool-box'].loadPools();
            },

            // 提交(确认)预约
            submit(){
                var vm = this;
                if (vm.selectedNoAppointRows.length > 0) {
                    var selectedPoolId = vm.selectedNoAppointRows[0].poolId;
                    if (!selectedPoolId) {
                        abp.notify.error('未设置号池');
                        return;
                    }
                    for (var i = 0; i < vm.selectedNoAppointRows.length; i++) {
                        if (!vm.selectedNoAppointRows[i].poolId) {
                            abp.notify.error('存在无号池检查项目');
                            return;
                        } else if (!selectedPoolId === vm.selectedNoAppointRows[i].poolId) {
                            abp.notify.error('存在多个不同号池');
                            return;
                        }
                    }
                    abp.message.confirm('预约将提交').then(confirmed => {
                        if (confirmed) {
                            abp.ajax({
                                type: 'post',
                                url: `/api/manual-appoint/subimt-appoint`,
                                data: JSON.stringify(vm.selectedNoAppointRows)
                            }).then(res => {
                                if (res.code === 0) {
                                    abp.notify.success(res.msg);
                                    vm.selectedPoolId = null;
                                    vm.noAppointSelectedPoolId = null;
                                    vm.selectedNoAppointRows = [];
                                    vm.noAppointPatientName = null;
                                    vm.loadNoAppointData(true).then(() => {
                                        vm.loadAppointedData(false);
                                    })
                                }
                            })
                        }
                    })
                } else {
                    abp.notify.error('无选中检查项目，请选中检查项目，并为所选项目选择号池。')
                }

            },

            // 确认并签到
            submitAndSignIn() {
                var vm = this;

            },

            // 提交预约修改
            submitEdit() {
                var vm = this;
                if (!vm.selectedAppointedRows.length > 0) {
                    abp.notify.error('无选中检查项目，请选中检查项目，并为所选项目选择号池。')
                    return;
                }
                for (var i = 0; i < vm.selectedAppointedRows.length; i++) {
                    var row = vm.selectedAppointedRows[i]
                    if (!row.poolId) {
                        abp.notify.error('预约号池不能为空');
                        return;
                    }
                    if (!row.edited) {
                        abp.notify.error('未设置新号池');
                        return;
                    }
                }
                abp.message.confirm('提交预约修改').then(confirmed => {
                    if (confirmed) {
                        abp.ajax({
                            type: 'post',
                            url: '/api/manual-appoint/submit-edit',
                            data: JSON.stringify(vm.selectedAppointedRows)
                        }).then(res => {
                            if (res.code === 0) {
                                vm.appointedSelectedPoolId = null;
                                vm.selectedPoolId = null;
                                //vm.$refs.appointed.clearSelection();
                                abp.notify.success(res.msg);
                                vm.loadAppointedData(false).then(() => {
                                    vm.$refs["pool-box"].loadPools();
                                })

                            }
                        })
                    }
                })
            },

            // 取消预约
            cancel() {
                var vm = this;
                if (!vm.selectedAppointedRows.length > 0) {
                    abp.notify.error('无选中检查项目，请选中检查项目，并为所选项目选择号池。')
                    return;
                }
                abp.message.confirm('取消检查项目预约')
                    .then(confirmed => {
                        if (confirmed) {
                            abp.ajax({
                                type: 'post',
                                url: '/api/manual-appoint/cancel',
                                data: JSON.stringify(vm.selectedAppointedRows)
                            }).then(res => {
                                if (res.code === 0) {
                                    vm.appointedPatinetName = null;
                                    vm.selectedPoolId = null;
                                    vm.selectedAppointedRows = [];
                                    vm.loadAppointedData(true).then(() => {
                                        vm.loadNoAppointData(false);
                                    });
                                }
                            })
                        }
                    })
            },

            // 控制行点击
            noApppointRowClick(row, event, colunm) {
                var vm = this;
                if (colunm.type === 'selection' || !vm.noAppointSelectableIds.includes(row.id)) {// 是复选框列
                    return;
                }
                
                vm.$refs.noAppoint.toggleRowSelection(row);
            },

            apppointedRowClick(row, event, colunm) {
                var vm = this;
                if (colunm.type === 'selection' || !vm.appointedSelectableIds.includes(row.id)) {// 是复选框列
                    return;
                }
                vm.$refs.appointed.toggleRowSelection(row);
            },

            tabClickedhandler(tab) {
                const vm = this;
                    if (tab.name !== 'noAppoints') {
                        vm.saveNoAppointStatus().then(() => {
                            vm.recoverAppointedStatus();
                        })
                    } else {
                        vm.SaveAppointedStatus().then(() => {
                            vm.recoverNoAppointStatus();
                        })
                    }
            },

            saveNoAppointStatus() {
                const vm = this;
                return new Promise((resolve) => {
                    if (vm.selectedNoAppointRows.length > 0) {
                        vm.noAppointDeviceType = vm.deviceTypeId;
                        vm.noAppointSelectedPoolName = vm.selectedPoolName;
                        vm.noAppointDeviceId = vm.deviceId;
                        vm.noAppointSelectedPoolId = vm.selectedPoolId;
                    } else {
                        vm.noAppointDeviceType = null;
                        vm.noAppointDeviceId = null;
                        vm.noAppointSelectedPoolId = null;
                        vm.noAppointSelectedPoolName = null;
                    }
                    vm.noAppointDate = vm.date;
                    resolve();
                })
            },

            recoverNoAppointStatus() {
                const vm = this;
                vm.deviceId = vm.noAppointDeviceId;
                vm.deviceTypeId = vm.noAppointDeviceType;
                vm.date = vm.noAppointDate;
                vm.selectedPoolId = vm.noAppointSelectedPoolId ? vm.noAppointSelectedPoolId : null;
                vm.selectedPoolName = vm.noAppointSelectedPoolName;
                
            },

            SaveAppointedStatus() {
                const vm = this;
                return new Promise(reslove => {
                    if (vm.selectedAppointedRows.length > 0) {
                        vm.appointedDeviceType = vm.deviceTypeId;
                        vm.appointedDeviceId = vm.deviceId;
                        vm.appointedSelectedPoolId = vm.selectedPoolId;
                        vm.appointedSelectedPoolName = vm.selectedPoolName;
                    } else {
                        vm.appointedDeviceType = null;
                        vm.appointedDeviceId = null;
                        vm.appointedSelectedPoolId = null;
                        vm.appointedSelectedPoolName = null;
                    }
                    vm.appointedDate = vm.date;
                    reslove();
                })
            },

            recoverAppointedStatus() {
                const vm = this;
                vm.deviceId = vm.appointedDeviceId;
                vm.deviceTypeId = vm.appointedDeviceType;
                vm.date = vm.appointedDate;
                vm.selectedPoolId = vm.appointedSelectedPoolId ? vm.appointedSelectedPoolId : null;
                vm.selectedPoolName = vm.appointedSelectedPoolName;
            },

            // 点击这点面板后改变表格高度
            clickCollapseHandler(array) {
                var vm = this;
                if (array) {
                    vm.tableHeight = 500;
                } else {
                    vm.tableHeight = 630;
                }
                vm.noAppoints = [...vm.noAppoints];
                vm.appointeds = [...vm.appointeds];
            },

            // 查询重置
            clearSearch() {
                var vm = this;
                Object.keys(vm.userData).forEach(key => {
                    if (key !== 'applyTimeRange') {
                        vm.userData[key] = null;
                    }
                });
                vm.number = null;
                vm.selectedRangeIndex = 0;
                vm.setRangeToday();
            },

            // 未预约，是否可选中  callback  (bool)
            getNSelectable(row, index) {
                var vm = this;
                return vm.noAppointSelectableIds.includes(row.id) ;
            },

            // 已预约，是否可选中  callback  (bool)
            getSelectable(row, index) {
                var vm = this;
                return vm.appointedSelectableIds.includes(row.id);
            },

            // 未预约列表选中改变
            noAppointSelectedChange(rows) {
                var vm = this;
                if (rows && rows.length > 0) {
                    vm.deviceTypeId = rows[0].deviceTypeId;
                    vm.patientName = rows[0].patientId;
                    vm.noAppointPatientName = vm.patientName;
                    vm.noAppointSelectableIds = vm.noAppoints.filter(item => item.deviceTypeId === vm.deviceTypeId && item.patientId === vm.patientName).map(item => item.id);
                    if (vm.selectedPoolName && vm.selectedPoolId) {
                        var idList = rows.map(row => row.id);
                        for (var i = 0; i < vm.noAppoints.length; i++) {
                            if (idList.includes(vm.noAppoints[i].id)) {
                                vm.noAppoints[i].appointment = vm.selectedPoolName;
                                vm.noAppoints[i].poolId = vm.selectedPoolId;
                                vm.noAppoints[i].edited = true;
                            } else {
                                vm.noAppoints[i].appointment = null;
                                vm.noAppoints[i].poolId = null;
                                vm.noAppoints[i].edited = false;
                            }
                        }
                    }
                } else {
                    vm.patientName = null;
                    vm.noAppointPatientName = null;
                    vm.selectedPoolName = null;
                    vm.selectedPoolId = null;
                    vm.noAppointSelectedPoolId = null;
                    vm.noAppointSelectableIds = vm.noAppoints.map(item => item.id);
                    vm.deviceTypeId = null;
                    for (var i = 0; i < vm.noAppoints.length; i++) {
                        vm.noAppoints[i].appointment = null;
                        vm.noAppoints[i].poolId = null;
                        vm.noAppoints[i].edited = false;
                    }
                };
                vm.selectedNoAppointRows = rows
                vm.noAppoints = [...vm.noAppoints];
            },

            // 已预约表格选中改变
            appointedSelectedChange(rows,old) {
                var vm = this;
                if (rows && rows.length > 0) {
                    vm.deviceTypeId = rows[0].deviceTypeId;
                    vm.patientName = rows[0].patientId;
                    vm.appointedPatinetName = vm.patientName;
                    vm.appointedSelectableIds = vm.appointeds.filter(item => item.deviceTypeId === vm.deviceTypeId && item.patientId === vm.patientName).map(item => item.id);
                    if (vm.selectedPoolName && vm.selectedPoolId) {  // 有选中号池
                        var idList = rows.map(row => row.id);
                        var oldRowIds = vm.selectedAppointedRows.map(e => e.id);
                        for (var i = 0; i < vm.appointeds.length; i++) {
                            if (idList.includes(vm.appointeds[i].id) && !oldRowIds.includes(vm.appointeds[i].id)) {  // 新选中的行
                                vm.appointeds[i].oldInfo = vm.appointeds[i].appointment;
                                vm.appointeds[i].oldPoolId = vm.appointeds[i].poolId;
                                vm.appointeds[i].appointment = vm.selectedPoolName;
                                vm.appointeds[i].poolId = vm.selectedPoolId;
                                vm.appointeds[i].edited = true;
                            } else if (!idList.includes(vm.appointeds[i].id) && oldRowIds.includes(vm.appointeds[i].id)) { // 取消选中的
                                vm.appointeds[i].appointment = vm.appointeds[i].oldInfo;
                                vm.appointeds[i].poolId = vm.appointeds[i].oldPoolId;;
                                vm.appointeds[i].edited = false;
                            }
                        }
                    }
                } else {
                    vm.patientName = null;
                    vm.appointedPatinetName = null;
                    vm.selectedPoolName = null;
                    vm.selectedPoolId = null;
                    vm.appointedSelectedPoolId = 0;
                    vm.appointedSelectableIds = vm.appointeds.map(item => item.id);
                    vm.deviceTypeId = null;
                    for (var i = 0; i < vm.appointeds.length; i++) {
                        vm.appointeds[i].appointment = vm.appointeds[i].oldInfo;
                        vm.appointeds[i].poolId = vm.appointeds[i].oldPoolId;
                        vm.appointeds[i].edited = false;
                    }
                }
                vm.selectedAppointedRows = rows;
                vm.appointeds = [...vm.appointeds];
            },

            // 申请科室选项
            loadApplyDepartmentOptions() {
                var vm = this;
                abp.ajax({
                    type: 'get',
                    url: '/api/manual-appoint/get-apply-dep-options'
                }).then(res => {
                    if (res.code === 0) {
                        vm.applyDepartmentOptions = res.data;
                    }
                });
            },

            // 快捷时间范围被点击
            rangeTagClicked(index, item) {
                var vm = this;
                if (index !== vm.selectedRangeIndex) {
                    switch (item.value) {
                        case 'today':
                            // 设置时间范围为今天
                            vm.setRangeToday();
                            break;

                        case 'in2days':
                            // 设置时间范围为昨天
                            vm.setRangeIn2Days();
                            break;

                        case 'in3days':
                            // 设置时间范围为前天
                            vm.setRangeIn3Days();
                            break;

                        case 'in1week':
                            // 设置时间范围为最近三天
                            vm.setRangeInWeek();
                            break;
                        case 'custom':
                            // 让时间范围选择器可用
                            vm.dateTimePickerDisable = false;
                            break;

                        default:
                            break;
                    }
                    vm.selectedRangeIndex = index;
                    vm.timeRangeOptions = [...vm.timeRangeOptions];
                }

            },

            // 加载未预约
            loadNoAppointData(isClickedButton) {
                var vm = this;
                vm.noAppointLoading = true;
                return new Promise(resolve => {
                    if (isClickedButton) {
                        vm.reserveSelectionNoAppoint = false;
                        vm.$refs.noAppoint.clearSelection();
                    }
                    abp.ajax({
                        type: 'post',
                        url: `/api/manual-appoint/get-no-appoint-apply-item-list`,
                        data: JSON.stringify({
                            number: vm.number,
                            userData: vm.userData
                        })
                    }).then(res => {
                        if (res.code === 0) {
                            if (isClickedButton) {
                                vm.noAppointSelectableIds = res.data.map(item => item.id);
                            }
                            else {
                                if (vm.noAppointPatientName) {
                                    vm.noAppointSelectableIds = res.data.filter(item => item.patientId === vm.noAppointPatientName)
                                        .map(item => item.id);
                                } else {
                                    vm.noAppointSelectableIds = res.data.map(item => item.id);
                                }
                            }
                            if (res.data.length > 0 && vm.selectedNoAppointRows.length > 0 && vm.noAppointSelectedPoolName && vm.noAppointSelectedPoolId) {
                                let idList = vm.selectedNoAppointRows.map(item => item.id);
                                for (var i = 0; i < res.data.length; i++) {
                                    if (idList.includes(res.data[i].id)) {
                                        res.data[i].appointment = vm.noAppointSelectedPoolName;
                                        res.data[i].poolId = vm.noAppointSelectedPoolId;
                                        res.data[i].edited = true;
                                    } else {
                                        res.data[i].edited = false;
                                    }
                                }
                            }
                            vm.noAppoints = res.data;
                            vm.noAppointLoading = false;
                        }
                    })
                    resolve();
                })
                
            },

            // 加载已预约
            loadAppointedData(isClickButton) {
                var vm = this;
                vm.appointedLoading = true;
                return new Promise(resolve => {
                    if (isClickButton) {
                        vm.reserveSelectionAppointed = false;
                        vm.$refs.appointed.clearSelection();
                    }
                    abp.ajax({
                        type: 'post',
                        url: `/api/manual-appoint/get-appointed-apply-item-list`,
                        data: JSON.stringify({
                            number: vm.number,
                            userData: vm.userData
                        })
                    }).then(res => {
                        if (res.code === 0) {
                            if (isClickButton) {
                                vm.appointedSelectableIds = res.data.map(item => item.id);
                            } else {
                                if (vm.appointedPatinetName) {
                                    vm.appointedSelectableIds = res.data.filter(item => item.patientId === vm.appointedPatinetName)
                                        .map(item => item.id)
                                } else {
                                    vm.appointedSelectableIds = res.data.map(item => item.id);
                                }
                            }
                            // 预约标注回填
                            if (res.data.length>0 && vm.selectedAppointedRows.length > 0 && vm.appointedSelectedPoolId) {
                                let idList = vm.selectedAppointedRows.map(item => item.id);
                                for (var i = 0; i < res.data.length; i++) {
                                    if (idList.includes(res.data[i].id)) {
                                        res.data[i].appointment = vm.appointedSelectedPoolName;
                                        res.data[i].poolId = vm.appointedSelectedPoolId;
                                        res.data[i].edited = true;
                                    } else {
                                        res.data[i].edited = false;
                                    }
                                }
                            }
                            vm.appointeds = res.data;
                            vm.appointedLoading = false;
                        }
                    })
                    resolve();
                });
                
            },

            // 加载当前用户角色和权限.
            loadPermission() {
                var vm = this;
                return new Promise((resolve, reject) => {
                    if (abp.auth.isGranted('DepAppointGroup.ManualAppoint')) { // 手工预约根权限
                        vm.manulaPermission = true;
                    }
                    resolve();
                });
            },

            // js日期格式化
            format(date) {
                return dayjs(date).format('YYYY-MM-DD HH:mm:ss')
            },

            setRangeToday() {
                var vm = this;
                var begin = new Date((new Date()).setHours(0, 0, 0, 0));
                var end = new Date((new Date()).setHours(23, 59, 59, 999));
                vm.userData.applyTimeRange = [begin, end];
                vm.dateTimePickerDisable = true;
            },

            setRangeIn2Days() {
                var vm = this;
                var begin = new Date((new Date()).setHours(0, 0, 0, 0));
                var end = new Date((new Date()).setHours(23, 59, 59, 999));
                begin = new Date(begin.setDate(begin.getDate() - 1));
                vm.userData.applyTimeRange = [begin, end];
                vm.dateTimePickerDisable = true;
            },
            setRangeIn3Days() {
                var vm = this;
                var begin = new Date((new Date()).setHours(0, 0, 0, 0));
                var end = new Date((new Date()).setHours(23, 59, 59, 999));
                begin = new Date(begin.setDate(begin.getDate() - 2));
                vm.userData.applyTimeRange = [begin, end];
                vm.dateTimePickerDisable = true;
            },

            setRangeInWeek() {
                var vm = this;
                var begin = new Date((new Date()).setHours(0, 0, 0, 0));
                var end = new Date((new Date()).setHours(23, 59, 59, 999));
                begin = new Date(begin.setDate(begin.getDate() - 6));
                vm.userData.applyTimeRange = [begin, end];
                vm.dateTimePickerDisable = true;
            },

            // 选中信息
            poolSelectedHandler(param) {
                if (param) {
                    vm.selectedPoolId = param.id ? param.id : null;
                    vm.selectedPoolName = param.name;
                    if (vm.activeTabName === 'noAppoints') { // 操作的是"未预约"表格
                        vm.noAppointSelectedPoolId = param.id;
                        // 设置选中行的"预约信息""
                        vm.selectedNoAppointRows.forEach(row => {
                            row.edited = true;
                            row.appointment = vm.selectedPoolName;
                            row.poolId = vm.selectedPoolId;
                            for (var i = 0; i < vm.noAppoints.length; i++) {
                                if (vm.noAppoints[i].examItemNumber === row.examItemNumber) {
                                    vm.noAppoints[i] = row;
                                }
                            }
                        });
                        vm.noAppoints = [...vm.noAppoints];
                    }
                    else {  // 当前操作的是"已预约"表格
                        vm.appointedSelectedPoolId = param.id;
                        // 设置选中行的 "预约信息"
                        vm.selectedAppointedRows.forEach(row => {
                            row.edited = true;
                            row.oldInfo = row.appointment;
                            row.oldPoolId = row.poolId;
                            row.appointment = vm.selectedPoolName;
                            row.poolId = vm.selectedPoolId;
                            for (var i = 0; i < vm.appointeds.length; i++) {
                                if (vm.appointeds[i].examItemNumber === row.examItemNumber) {
                                    vm.appointeds[i] = row;
                                }
                            }
                        });
                        vm.appointeds = [...vm.appointeds];
                    }
                } else {
                    vm.selectedPoolId = null;
                    vm.selectedPoolName = null;
                }
            },

            // 预约日期限制
            getDateLimit() {
                const vm = this;
                abp.ajax({
                    type: 'get',
                    url: `/api/manual-appoint/get-date-limit?deviceId=${vm.deviceId}`
                }).then(res => {
                    if (res.code === 0) {
                        vm.limitDay = res.data;
                    }
                })
            },

            // 初始化
            init() {
                var vm = this;
                vm.defaultAppointDate = new Date();
                vm.date = new Date();
                vm.noAppointDate = vm.date;
                vm.appointedDate = vm.date;
                vm.setRangeToday();
                vm.loadPermission()
                    .then(() => {
                        vm.loadApplyDepartmentOptions();
                    });
            }
        },
        created() {
            var vm = this;
        },
        mounted() {
            var vm = this;
            vm.init();
        }
    });
});